<template>
	<view class="content">
		<uni-list>
			<uni-list :border="true">
				<!-- 右侧带角标 -->
				<uni-list-chat v-for="(item,index) in dialogueList" :title="item.toUserNickName" :avatar-circle="true"
					:avatar="item.toUserHead" :note="item.latestTimeMessageContent" :time="item.createTime"
					:badge-text="item.unReadCount" :clickable="true"
					@click="clickDialogue(item.toUserId,item.id)"></uni-list-chat>
			</uni-list>
		</uni-list>
		<view>
			<!-- 提示窗示例 -->
			<uni-popup ref="alertDialog" type="dialog">
				<uni-popup-dialog :type="msgType" cancelText="关闭" confirmText="去登录" title="提示" content="请先登录!"
					@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import {
		getDialogueList,
		getUnread
	} from "@/api/dialogue.js"
	export default {
		data() {
			return {
				theFirst: true,
				msgType: 'success',
				dialogueList: [],
				currentUserId: 0
			}
		},
		onReady() {
			this.hasToken()
			this.theFirst = false
			uni.getStorage({
				key: "userVO",
				success: (res) => {
					this.currentUserId = res.data.id;
				}
			})
		},
		onShow() {
			if (!this.theFirst) {
				this.hasToken()
			}
			getDialogueList().then(res => {
				this.dialogueList = res.data;
			})

		},
		methods: {
			hasToken() {
				let token = '';
				uni.getStorage({
					key: 'token',
					success: function(res) {
						token = res.data;
					}
				});
				// 若用户未登录则跳一个弹窗
				if (token == '' || token == null) {
					this.dialogToggle("warn");
				}
			},
			dialogToggle(type) {
				this.msgType = type;
				this.$refs.alertDialog.open()
			},
			dialogConfirm() {
				console.log('点击确认')
				uni.navigateTo({
					url: "/pages/login/index"
				})
			},
			dialogClose() {
				console.log('点击关闭')
				uni.switchTab({
					url: "/pages/index/index"
				})
			},
			clickDialogue(toUserId, dialogueId) {
				uni.navigateTo({
					url: "/pages/chat_room/index?currentUserId=" + this.currentUserId + "&toUserId=" + toUserId +
						"&dialogueId=" + dialogueId
				})
			}
		}
	}
</script>

<style>
	.chat-custom-right {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-end;
	}

	.chat-custom-text {
		font-size: 12px;
		color: #999;
	}
</style>